#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
mt19937 rnd(time(0));
const int MAXN=2e5+5;
const int B=2505;
bool Mbe;
namespace IO{
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){
	int re=0;char ch=getchar();
	while(ch>'9'||ch<'0') ch=getchar();
	while(ch>='0'&&ch<='9') re=10*re+ch-'0',ch=getchar();
	return re; 
}
inline void write(ll x){
	static short stk[10],top;
	if(x<=9){putchar(x+'0');return;}
	top=0;
	while(x) stk[++top]=x%10,x/=10;
	for(int i=top;i>=1;i--) putchar(stk[i]+'0'); 
}
}using namespace IO;
int ev[MAXN<<2],en[MAXN<<2],nxt[MAXN],tot;
int n,q,c,cnt,hd,tl;short a[MAXN];
int qs[MAXN],qu[MAXN],d[MAXN],ans[MAXN],siz[MAXN],p[1<<15|10];
int qn[MAXN],qfa[MAXN];
bool Med;
inline void update(const int &u){
	cnt++;
	memset(p,0,sizeof(p));
	qn[hd=tl=1]=u;d[u]=(1<<a[u]);
	while(hd<=tl){
		const int fa=qfa[hd],u=qn[hd++];
		p[d[u]]++;
		for(int i=nxt[u];i;i=en[i]){
			const int &v=ev[i];
			if(v!=fa){
				d[v]=(d[u]|(1<<a[v]));
				qn[++tl]=v,qfa[tl]=u;
			}
		}
	}
	for(int i=1;i<=c;i++) for(int j=0;j<(1<<c);j+=(1<<i))
		for(int k=j;k<(j|(1<<i-1));k++) 
			p[k|(1<<i-1)]+=p[k];
	for(int i=1;i<=q;i++)
		if((d[qu[i]]|qs[i])==qs[i]) ans[i]=p[qs[i]];
} 
inline void dfs(const int &u,const int &fa){
	siz[u]=1;
	for(int i=nxt[u];i;i=en[i]){
		const int &v=ev[i];
		if(v!=fa){ 
			dfs(v,u);
			siz[u]+=siz[v];
		} 
	} 
	if(siz[u]>B) siz[u]=1,update(u);
}
int main(){
	freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);

	// cerr<<"Memory: "<<(&Mbe-&Med)*1.0/1024/1024<<endl;
	n=read(),q=read(),c=read();
	for(int i=1;i<=n;i++) a[i]=read();
	auto link=[&](int u,int v){
		tot++;
		ev[tot]=v,en[tot]=nxt[u];nxt[u]=tot;
	};
	for(int i=2,f;i<=n;i++) f=read(),link(i,f),link(f,i);
	for(int i=1;i<=q;i++) qs[i]=read(),qu[i]=read();
	dfs(1,0);
	for(int i=1;i<=q;i++)if(!ans[i]){
		qn[hd=tl=1]=qu[i];d[qu[i]]=(1<<a[qu[i]]);
		while(hd<=tl){
			const int fa=qfa[hd],u=qn[hd++];
			for(int j=nxt[u];j;j=en[j]){
				const int &v=ev[j]; 
				if(v!=fa){
					d[v]=(d[u]|(1<<a[v]));
					if((d[v]|qs[i])==qs[i]) qn[++tl]=v,qfa[tl]=u;
				}
			} 
		}
		ans[i]=tl;
	}
	for(int i=1;i<=q;i++) write(ans[i]),putchar('\n'); 
}